{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Present value/future value of money "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Time value of money\n",
    "\n",
    "The important concept in finance is that of the time value of money $x today is worth more than $x in a year's time!!!\n",
    "\n",
    "_Why_? because we may invest $x today\n",
    "       -> we may invest it into stocks\n",
    "       -> we can lend it to the bank for some interest\n",
    "Future money worth less because of inflation will push prices higher so the buying power of your money will be lower!\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Future Value (FV)\n",
    "**Future Value** is the vlaue of a current asset at a specified date in the future based on assumed rate of growth over set time\n",
    "\n",
    "- Future Value = x (1 + r)^n\n",
    "\n",
    "**Present Value** is how much a future sum of money is worth today given a specified rate of interest\n",
    "\n",
    "- Present Value = x / (1 + r)^n\n",
    "\n",
    "\n",
    "r: interest rate (.05 to 5%)\n",
    "\n",
    "x: cash flow in future\n",
    "\n",
    "n: number of years"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Continuous Model w/ Differential equations\n",
    "\n",
    "Suppose we have amount x(t) in the bank in time t. How much does this increase in value from one day to the next?\n",
    "                        \n",
    "                            /-Taylor-Expansion\n",
    "    x(t+dt) - x(t) = dx(t)/dt * dt\n",
    "       \\-The cange in the amount of money within a dt day\n",
    "       \n",
    "But the interest I recieve must be proportional to the actual x(t) amount I have and the r interest rate and the dt time step!\n",
    "\n",
    "    x(t+dt) - x(t) = dx(t)/dt * dt = r x(t) dt\n",
    "    \n",
    "    dx(t)/dt = r x(t) *The soultion is x(t) = x(0)e^(r*t)\n",
    "\n",
    "- Continuous Future = x(0)e^(r\\*t)\n",
    "- Continuous Present = x(0) / e^(r\\*t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Time value of money implementation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Value of $100\n",
      "Future Discrete:  101.0\n",
      "Future Continuous:  101.00501670841679\n",
      "Present Discrete:  99.00990099009901\n",
      "Present Continuous:  99.00498337491682\n"
     ]
    }
   ],
   "source": [
    "from math import exp\n",
    "     \n",
    "def discreteFutureValue(x,r,n):\n",
    "    return x * (1+r)**n\n",
    "\n",
    "def discretePresentValue(x,r,n):\n",
    "    return x / (1+r)**n\n",
    "\n",
    "def contFutureValue(x,r,n):\n",
    "    return x * exp(r*n)\n",
    "\n",
    "def contPresentValue(x,r,n):\n",
    "    return x / exp(r*n)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    x = 100  # value of investment in dollars\n",
    "    r = .01 # interest rate\n",
    "    n = 1    # years\n",
    "    print(\"Value of $100\")\n",
    "    print(\"Future Discrete: \", discreteFutureValue(x,r,n))\n",
    "    print(\"Future Continuous: \", contFutureValue(x,r,n))\n",
    "    print(\"Present Discrete: \", discretePresentValue(x,r,n))\n",
    "    print(\"Present Continuous: \", contPresentValue(x,r,n))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stock Market Basics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Stocks represent a small piece of ownship in the company\n",
    "\n",
    "Allows companies to sell ownership in an effort to raise money\n",
    "\n",
    "The shareholder gives money for a sontract stating how much of the company he owns\n",
    "\n",
    "**Why is this important**\n",
    "- Dividends + any growth of the stock's value\n",
    "  (Dividends are payments made quaterly/bi-annually to shareholds, depends    on the profitability of the given company)\n",
    "\n",
    "**What is volatility?**\n",
    "\n",
    "- Statistical measurement of the dispersion of returns for a given    security\n",
    "- This is the amount of uncertainty/risk about the size of changes in the value of a given security (stock,bond, etc.)\n",
    "\n",
    "- We can measure volatility with standard deviation or variance between returns of the same security\n",
    "\n",
    "**Higher volatility == Higher risk**\n",
    "\n",
    "*How to approximate volatility?* With the **CAPM** Beta value\n",
    "\n",
    "__**CAPM - Capital Assetment Pricing Mangement**__\n",
    "\n",
    "Aim of stocks is to predict and trade future values \n",
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Commodities\n",
    "\n",
    "Commodities are raw products: gold, silver,oil\n",
    "\n",
    "These are traded on the FUTURE market\n",
    "- The price of these are unpredictable, but show seasonal trends/effects\n",
    "- Scarcit of the product results in higher prices and vice-versa\n",
    "- Most trading is done on the future market. You can make deals to buy/sell a given commodity at some time in the future."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Currencies\n",
    "\n",
    "FOREX = Foreign Exchange\n",
    "\n",
    "- the top currencies to considerL USD, CAD, AUD, YEN and EUR\n",
    "- money is made by some mispricings: you can make arbitrage profits by exploiting these mispricings\n",
    "- Bellman-Ford algorithm\n",
    "- Fluctuation in exchange rates is unpredictable BUT it has something to do with interest rates\n",
    "- Bellman-Ford builds directed graph and performs shortest path to find arbitrage opportunities\n",
    "\n",
    "### Positions\n",
    "\n",
    "Long -> For long periods of ownership\n",
    "Short -> Expectation that the stock with momentarily inscrease before decreasing\n",
    "\n",
    "### Bonds \n",
    "Abond is a debt investment in which an investor loans money to an entity (company or government)\n",
    "- for a defined period of time\n",
    "- variable or fixed interest rate\n",
    "\n",
    "**BONDS ARE FIXED-INCOME SECURITIES**\n",
    "\n",
    "- interest rate of bonds are a bit higher than banks\n",
    "\n",
    "- Agencies assign ratings to bond issuers: if the issuer has poor credit -> risk of default is greater\n",
    "- The longer the bond maturity, greater the porbability of default\n",
    "- So bonds with longer time to maturity typically have higher interest rates\n",
    "\n",
    "**Bonds in general are less risk adverse than stocks**\n",
    "\n",
    "### Bond Price & Interest Rates \n",
    "\n",
    "Bonds Pricing\n",
    "\n",
    "par value - \\$1000\n",
    "10% rate/coupon\n",
    "2 years \n",
    "\n",
    "What does this mean?\n",
    "In two years the buyer will recieve \\$1000 plus 10% premium\n",
    "\n",
    "*How much would an investor pay for a bond? *\n",
    "We have to calculate the present value of that future cash flow (because we get \\$1000 in the future!) This reinforces that the most important concept in finance is that of the time value of money \\$x today is worth more than \\$x in the future\n",
    "\n",
    "There is an inverse relationship between interest rates and bond prices \n",
    "- if interest rates rise -> bond prices decline\n",
    "\n",
    "Present Value = 1000 / (1+.10)^2\n",
    "\n",
    "Present Value = 826.4462809917354\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bond price and Maturity\n",
    "\n",
    "two bonds:\n",
    "Bond A expires in 1 year\n",
    "Bond B expires in 10 years\n",
    "\n",
    "The interest rate (coupon) for bond V will be greater than for bond A... investors bear more risk for greater return. The longer the maturity the greater the risk something will happen to the company.\n",
    "\n",
    "### Bond Pricing Implementation\n",
    "\n",
    "Two types of bonds: \n",
    "\n",
    "- Zero-coupon bonds: we get the fixed value of bond at matrity\n",
    "Bond price = Present Value = par_value / (1 + marketRate)^n\n",
    "\n",
    "- Coupon bonds: we bond + coupon\n",
    "Bond price = (C/R)(1-(1/(1+r)^n)) + par_value/(1+r)^n\n",
    "\n",
    "\\*C: annual coupon payment \n",
    "\\*r: Interest Rate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Price of the zero-coupon bond: $889.00\n",
      "Price of the coupon bond: $1027.75\n"
     ]
    }
   ],
   "source": [
    "from math import exp\n",
    "\n",
    "def zeroBondPrice(par_value,market_rate,n):\n",
    "    return par_value/(1+market_rate)**n\n",
    "\n",
    "def bondPrice(par_value,coupon, market_Rate,n):\n",
    "    c = par_value *coupon\n",
    "    return c/market_rate*(1-(1/(1+market_rate)**n))+ par_value/(1+market_rate)**n\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    par_value = 1000   # par value of the bond\n",
    "    coupon = 0.05      # bond yield - coupon\n",
    "    n = 3              # number of years \n",
    "    market_rate = 0.04 #market rate of return\n",
    "    \n",
    "    print(\"Price of the zero-coupon bond: $%0.2f\" % zeroBondPrice(par_value,market_rate,n))\n",
    "    print(\"Price of the coupon bond: $%0.2f\" % bondPrice(par_value, coupon,market_rate,n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
